package com.fuxi.cloud.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Pattern;

/**
 * ReflectHelper
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */

public class ReflectHelper {
    private static final Logger log = LoggerFactory.getLogger(ReflectHelper.class);
    private Class cls;
    private Object obj;
    private Hashtable<String, Method> getMethods = null;
    private Hashtable<String, Method> setMethods = null;

    public ReflectHelper(Object o) {
        this.obj = o;
        this.initMethods();
    }

    public void initMethods() {
        this.getMethods = new Hashtable();
        this.setMethods = new Hashtable();
        this.cls = this.obj.getClass();
        Method[] var1 = this.cls.getMethods();
        String var2 = "get(\\w+)";
        Pattern var3 = Pattern.compile(var2);
        String var4 = "set(\\w+)";
        Pattern var5 = Pattern.compile(var4);
        String var6 = "$1";

        for(int var8 = 0; var8 < var1.length; ++var8) {
            Method var9 = var1[var8];
            String var10 = var9.getName();
            String var7;
            if (Pattern.matches(var2, var10)) {
                var7 = var3.matcher(var10).replaceAll(var6).toLowerCase();
                this.getMethods.put(var7, var9);
            } else if (Pattern.matches(var4, var10)) {
                var7 = var5.matcher(var10).replaceAll(var6).toLowerCase();
                this.setMethods.put(var7, var9);
            }
        }

    }

    public boolean setMethodValue(String property, Object object) {
        Method var3 = (Method)this.setMethods.get(property.toLowerCase());
        if (var3 != null) {
            try {
                var3.invoke(this.obj, object);
                return true;
            } catch (Exception var5) {
                log.error("invoke getter on " + property + " error: " + var5.toString());
                return false;
            }
        } else {
            return false;
        }
    }

    public Object getMethodValue(String property) {
        Object var2 = null;
        Method var3 = (Method)this.getMethods.get(property.toLowerCase());
        if (var3 != null) {
            try {
                var2 = var3.invoke(this.obj);
            } catch (Exception var5) {
                log.error("invoke getter on " + property + " error: " + var5.toString());
            }
        }

        return var2;
    }

    public Object setAll(Map<String, Object> data) {
        if (data != null && data.keySet().size() > 0) {
            Iterator var2 = data.entrySet().iterator();

            while(var2.hasNext()) {
                Map.Entry var3 = (Map.Entry)var2.next();
                this.setMethodValue((String)var3.getKey(), var3.getValue());
            }

            return this.obj;
        } else {
            return null;
        }
    }

    public static Object setAll(Object o, Map<String, Object> data) {
        ReflectHelper var2 = new ReflectHelper(o);
        var2.setAll(data);
        return o;
    }

    public static <T> T setAll(Class<T> clazz, Map<String, Object> data) {
        Object var2 = null;

        try {
            var2 = clazz.newInstance();
        } catch (Exception var4) {
            var4.printStackTrace();
            var2 = null;
            return (T) var2;
        }

        return (T) setAll(var2, data);
    }

    public static <T> List<T> transList2Entrys(List<Map<String, Object>> mapist, Class<T> clazz) {
        ArrayList var2 = new ArrayList();
        if (mapist != null && mapist.size() > 0) {
            Iterator var3 = mapist.iterator();

            while(var3.hasNext()) {
                Map var4 = (Map)var3.next();
                var2.add(setAll(clazz, var4));
            }
        }

        return var2;
    }

    public static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String var2 = fieldName.substring(0, 1).toUpperCase();
            String var3 = "get" + var2 + fieldName.substring(1);
            Method var4 = o.getClass().getMethod(var3);
            Object var5 = var4.invoke(o);
            return var5;
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static String[] getFiledName(Object o) {
        Field[] var1 = o.getClass().getDeclaredFields();
        String[] var2 = new String[var1.length];

        for(int var3 = 0; var3 < var1.length; ++var3) {
            var2[var3] = var1[var3].getName();
        }

        return var2;
    }

    public static List<Map> getFiledsInfo(Object o) {
        Field[] var1 = o.getClass().getDeclaredFields();
        String[] var2 = new String[var1.length];
        ArrayList var3 = new ArrayList();
        HashMap var4 = null;

        for(int var5 = 0; var5 < var1.length; ++var5) {
            var4 = new HashMap();
            var4.put("type", var1[var5].getType().toString());
            var4.put("name", var1[var5].getName());
            var4.put("value", getFieldValueByName(var1[var5].getName(), o));
            var3.add(var4);
        }

        return var3;
    }

    public static Object[] getFiledValues(Object o) {
        String[] var1 = getFiledName(o);
        Object[] var2 = new Object[var1.length];

        for(int var3 = 0; var3 < var1.length; ++var3) {
            var2[var3] = getFieldValueByName(var1[var3], o);
        }

        return var2;
    }
}
